package com.eascs.notice.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.eascs.admin.common.config.FtpConfig;
import com.eascs.admin.common.utils.FtpNetUtils;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.file.form.FileForm;
import com.eascs.notice.entity.FormNoticeLetterEntity;
import com.eascs.notice.entity.OaDataListFileEntity;
import com.eascs.notice.service.O2oNoticeLetterService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;

@RestController
@RequestMapping("/rnotice")
public class O2oNoticeLetterRestController extends BaseRestController {

	@Autowired
	O2oNoticeLetterService o2onoticeLetterservice;

	@RequestMapping(value = "/oafile/{datalist}/{billcode}")
	public BaseRestResult<String> oaLoanUpload(@RequestParam MultipartFile file, @PathVariable("datalist") String datalist, @PathVariable("billcode") String billcode) {
		o2onoticeLetterservice.oaUpload(file, datalist, billcode);
		return BaseRestResult.refresh();
	}

	@RequestMapping(value = "/o2ofile/{datalist}/{billcode}")
	public BaseRestResult<String> o2oLoanUpload(@RequestParam MultipartFile file, @PathVariable("datalist") String datalist, @PathVariable("billcode") String billcode) {
		o2onoticeLetterservice.o2oUpload(file, datalist, billcode);
		return BaseRestResult.refresh();
	}

	@RequestMapping(value = "/file/oadownload")
	public void download(FileForm fileForm, HttpServletResponse httpServletResponse) {

		OaDataListFileEntity attachment = o2onoticeLetterservice.findOaDataListFileEntity(fileForm.getDatalistno());
		// 扩展名和文件名
		String filename = attachment.getOriginalName();
		try {
			filename = URLEncoder.encode(filename, "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		String extname = attachment.getExtendName();
		if ("bmp,jpeg,gif".indexOf(extname) != -1) {
			httpServletResponse.setContentType("image/" + extname);
		} else if ("jpg".equals(extname)) {
			httpServletResponse.setContentType("image/jpeg");
		} else if ("xls".equals(extname)) {
			httpServletResponse.setContentType("application/vnd.ms-excel");
			httpServletResponse.setHeader("Content-disposition", "attachment; filename=" + filename);
		} else if ("doc".equals(extname)) {
			httpServletResponse.setContentType("application/msword; charset=UTF-8");
			httpServletResponse.setHeader("Content-disposition", "attachment; filename=" + filename);
		} else if ("ppt".equals(extname)) {
			httpServletResponse.setContentType("application/vnd.ms-powerpoint; charset=UTF-8");
			httpServletResponse.setHeader("Content-disposition", "attachment; filename=" + filename);
		} else {
			httpServletResponse.setHeader("Content-disposition", "attachment; filename=" + filename);
		}
		// 输出流
		try {
			boolean s = FtpNetUtils.downFile1(FtpConfig.ftpConfigSetting().getHref(), Integer.valueOf(FtpConfig.ftpConfigSetting().getPort()), FtpConfig.ftpConfigSetting().getUsername(), FtpConfig.ftpConfigSetting().getPassword(), attachment.getUrlFilePath().substring(0, attachment.getUrlFilePath().lastIndexOf("/") + 1), attachment.getUrlFilePath().substring(attachment.getUrlFilePath().lastIndexOf("/") + 1, attachment.getUrlFilePath().length()), httpServletResponse);
			if (!s) {
				ServletOutputStream outs = httpServletResponse.getOutputStream();
				httpServletResponse.setContentType("text/html;charset=UTF-8");
				StringBuffer stringBuffer = new StringBuffer();
				outs.print(new String(stringBuffer.toString().getBytes("UTF-8"), "utf-8"));
				outs.flush();
				outs.close();
			}
		} catch (Exception e) {
		}
	}

	@SuppressWarnings("unused")
	@RequestMapping("/oabasic")
	public PaginationRestResult getOaBasicData(FormNoticeLetterEntity entity) {
		// @o2o
		entity.setPageSize(10);

		// @OA
		PageResult<Object[]> oapageResult = o2onoticeLetterservice.getOaBasicDataLoan(entity);
		String[] oatitles = { "申请单号", "客户名称", "经营单位", "借款人姓名", "还款方式", "利率(%)/年", "期限(月)", "申请金额(元)", "银行批复金额(元)", "贷款信贷经理", "是否上传附件" };
		String oapageUrl = "/rnotice/oabasic";
		if (entity != null) {
			String billCode = entity.getBill_code() == null ? "" : entity.getBill_code();
			String userName = entity.getName() == null ? "" : entity.getName();
			String isUploadFile = entity.getIsUploadFile() == null ? "" : entity.getIsUploadFile();
			oapageUrl = UrlUtil.appendUrl(oapageUrl.toString(), "bill_code", billCode);
			oapageUrl = UrlUtil.appendUrl(oapageUrl, "name", userName);
			oapageUrl = UrlUtil.appendUrl(oapageUrl, "isUploadFile", isUploadFile);
			oapageUrl = oapageUrl + "&showPage=";
		} else {
			oapageUrl = oapageUrl + "?showPage=";
		}

		PaginationInfo oapaginationInfo = new PaginationInfo(oapageResult, oatitles, oapageUrl);
		oapaginationInfo.setBuildPagingNavigation(true);
		oapaginationInfo.setFirstRenderColumnIndex(0);
		Button[] oafindButton = new Button[] { ButtonHelper.createRedirectButton(null, "/notice/oaextend?billCode="), null };
		oapaginationInfo.setTitleButtons(oafindButton);
//		oapaginationInfo.setMoreButtons(oafindButton);
		oapaginationInfo.setRendTo("#oa_result");
		ViewContext.put("vo", entity);
		return PaginationRestResult.buildPaginationRestResult(oapaginationInfo);
	}

	@SuppressWarnings("unused")
	@RequestMapping("/o2obasic")
	public PaginationRestResult getO2oBasicData(FormNoticeLetterEntity entity) {
		// @o2o
		entity.setPageSize(10);
		PageResult<Object[]> o2opageResult = o2onoticeLetterservice.getBasicDataLoan(entity);
		String[] o2otitles = { "申请单号", "客户名称", "经营单位", "借款人姓名", "还款方式", "利率(%)/年", "期限(月)", "申请金额(元)", "银行批复金额(元)", "贷款信贷经理", "是否上传附件" };
		String o2opageUrl = "/rnotice/o2obasic";
		if (entity != null) {
			String billCode = entity.getBill_code() == null ? "" : entity.getBill_code();
			String userName = entity.getName() == null ? "" : entity.getName();
			String isUploadFile = entity.getIsUploadFile() == null ? "" : entity.getIsUploadFile();
			o2opageUrl = UrlUtil.appendUrl(o2opageUrl, "bill_code", billCode);
			o2opageUrl = UrlUtil.appendUrl(o2opageUrl, "name", userName);
			o2opageUrl = UrlUtil.appendUrl(o2opageUrl, "isUploadFile", isUploadFile);
			o2opageUrl = o2opageUrl + "&showPage1=";
		} else {
			o2opageUrl = o2opageUrl + "?showPage1=";
		}

		PaginationInfo paginationInfo = new PaginationInfo(o2opageResult, o2otitles, o2opageUrl);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(0);
		Button[] findButton = new Button[] { ButtonHelper.createRedirectButton(null, "/notice/extend?billCode="), null };
		//paginationInfo.setTitleButtons(findButton);
		SelectDataOption []selectDataOptions = { null, null, null, null, new SelectDataOption("OTOREPAYMENTWAY"), null, null, null, null, null, new SelectDataOption("ISUPLOADFILE") };
		paginationInfo.setSelectDataOptions(selectDataOptions);
		paginationInfo.setRendTo("#o2o_result");
		ViewContext.put("vo", entity);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}
}
